const wait = (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms));
var cols = col1 = col2 = col3 = null;
var panelLog = null;
var queue = [];
var times = null;
/**
 * 汉诺塔算法
 * @param {Object} disc
 * @param {Object} col1
 * @param {Object} col2
 * @param {Object} col3
 */
var hanoi = function(disc, col1, col2, col3, cb) {
	console.log('col1:',col1.name,'col2:',col2.name,'col3:',col3.name);
	if(disc <= 0){
		return;
	}
	hanoi(disc - 1, col1, col3, col2, cb);
	cb(disc, col1, col3);
	hanoi(disc - 1, col2, col1, col3, cb);

}
//创建碟片
const createDisc = (index, total) => {
	var d = document.createElement('div');
	d.className = "disc disc-" + index;
	d.dataset.size = index;
	var i = total - index;
	d.style.bottom = 20 * i + 'px';
	d.style.width = (132 - 12 * i) + 'px';
	return d;
}

//移动碟片
const moveDisc = (disc, colFrom, colTo) => {
	var firstF = colFrom.childNodes[0];
	var firstT = colTo.childNodes[0];
	var sizeF = parseInt(firstF.dataset.size);
	var sizeT = 0;
	if(firstT !== undefined) {
		sizeT = parseInt(firstT.dataset.size);
	}
	if(sizeT !== 0 && sizeF > sizeT) {
		throw new Error('碟片必须小的在大的上面');
	}
	var lenT = colTo.childNodes.length;
	firstF.style.bottom = 20 * lenT + 'px';
	colTo.prepend(firstF);
}

const createLog = (content) => {
	var p = document.createElement('p');
	p.className = 'log';
	p.innerText = content;
	panelLog.prepend(p);
}

//初始化所有状态值
const init = (discCount, col1) => {
	col1.innerHTML = "";
	col2.innerHTML = "";
	col3.innerHTML = "";
	panelLog.innerHTML = "";
	queue = [];
	clearInterval(timer);
	timer = null;
	times.innerText = 0;
	var frag = new DocumentFragment();
	var i = 1;
	while(i <= discCount) {
		frag.appendChild(createDisc(i, discCount));
		i += 1;
	}
	col1.appendChild(frag);
}

var timer = null;
var runAnimation = (disc, col1, col3) => {
	queue.push({
		disc: disc,
		colFrom: col1,
		colTo: col3
	});
	if(timer === null) {
		timer = setInterval(() => {
			if(queue.length === 0) {
				clearInterval(timer);
				alert('运行完毕');
				return;
			}
			var curr = queue.shift();
			//			var content = ['Move disc ', curr.disc, ' from ', curr.colFrom.name, ' to ', curr.colTo.name].join('');
			var content = ['把碟片 ', curr.disc, ' 从柱子 ', curr.colFrom.name, ' 移动到柱子 ', curr.colTo.name].join('');
//			console.log(content);
			createLog(content);
			moveDisc(curr.disc, curr.colFrom, curr.colTo);
			times.innerText = parseInt(times.innerText) + 1;
		}, 500);
	}
}

window.onload = () => {
	discCount = 3;
	cols = document.querySelectorAll('.column');
	col1 = cols[0];
	col2 = cols[1];
	col3 = cols[2];
	col1.name = "1";
	col2.name = "2";
	col3.name = "3";
	times = document.querySelector('#times');
	panelLog = document.querySelector('#panel-log');
	var btnOk = document.querySelector('#btn-ok');
	var btnRun = document.querySelector('#btn-run');
	var disc = document.querySelector('#disc');
	btnRun.onclick = () => {
		discCount = parseInt(disc.value);
		if(discCount > 10) {
			alert('最多碟片只能为10个');
			return;
		}
		init(discCount, col1);
		hanoi(discCount, col1, col2, col3, runAnimation);
	};
	init(discCount, col1);
}